import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class FlightDataPanel extends JPanel{

	private ArrivalsTableModel arrivalsTableModel;	//this is the data container for all the planes arriving
	private DeparturesTableModel departuresTableModel; //this is the data container for all the planes department

	//ARRIVING PLANE ACTIONS
	private JButton clearToLand;
	private JButton messagePilotButton;

	private JButton notifyOtherUsersButton;
	private JButton messagePilotButton2;

	//DEPARTING PLANE ACTIONS
	private JButton clearForTakeoffButton;



	public FlightDataPanel() {

		// initialize the ui components
		initUI();
	}

	public void initUI() {
		// set the layout
		setLayout(new GridLayout(3,1));

		//create the buttons
		clearToLand = new JButton("Clear to Land");
		messagePilotButton = new JButton("Message Pilot");

		clearForTakeoffButton = new JButton("Clear for Takeoff");
		messagePilotButton2 = new JButton("Message Pilot");

		// Create arrivals table
		arrivalsTableModel = new ArrivalsTableModel();
    	JTable arrivalsTable = new JTable(arrivalsTableModel);
    	arrivalsTable.setPreferredScrollableViewportSize(new Dimension(600, 60));

    	// create arrivals scroll pane and add the arrivals table to it
    	JScrollPane arrivalsScrollPane = new JScrollPane(arrivalsTable);

    	// create arrivals actions panel
    	JPanel arrivalsActionsPanel = new JPanel();
    	arrivalsActionsPanel.add(clearToLand);
    	arrivalsActionsPanel.add(messagePilotButton);

    	// create arrivals panel and add the arrivals scroll pane and actions panel to it
    	JPanel arrivalsPanel = new JPanel();
    	arrivalsPanel.setLayout(new BoxLayout(arrivalsPanel, BoxLayout.Y_AXIS));
    	arrivalsPanel.setBorder(BorderFactory.createTitledBorder("Arrivals"));
    	arrivalsPanel.add(arrivalsScrollPane);
    	arrivalsPanel.add(arrivalsActionsPanel);

    	// create departures table
    	departuresTableModel = new DeparturesTableModel();
    	JTable departuresTable = new JTable(departuresTableModel);
    	departuresTable.setPreferredScrollableViewportSize(new Dimension(600, 60));

    	// create departures scroll pane and add the departures table to it
    	JScrollPane departuresScrollPane = new JScrollPane(departuresTable);

    	// create departures actions panel
    	JPanel departuresActionsPanel = new JPanel();
    	departuresActionsPanel.add(clearForTakeoffButton);
    	departuresActionsPanel.add(messagePilotButton2);

    	// create departures panel and add the departures scroll pane and actions panel to it
    	JPanel departuresPanel = new JPanel();
    	departuresPanel.setLayout(new BoxLayout(departuresPanel, BoxLayout.Y_AXIS));
    	departuresPanel.setBorder(BorderFactory.createTitledBorder("Departures"));
    	departuresPanel.add(departuresScrollPane);
    	departuresPanel.add(departuresActionsPanel);


		add(arrivalsPanel);
		add(departuresPanel);
		add(new RiskMitigationPanel());
	}

	// All table information goes in here.
    class ArrivalsTableModel extends AbstractTableModel {
    	// Three different columns:  User's name, position, and online status
    	String[] columnNames = {"Plane ID", "Origin", "Runway", "Location", "Status","Ground Control Clearance","Local Control Clearance","Traffic Control Clearance"};

    	// Sample data.  Data entered is a two dimensional array of objects.
    	Object[][] data = {
    			{"GFS102","Hobby","KAP303","Runway 3","Landing",new Boolean(true),new Boolean(false),new Boolean(true)},
    			{"SGM304","JFK","SAL200","Runway 4","In Air",new Boolean(true),new Boolean(false),new Boolean(true)},
    	};

    	// Essential functions

        public int getColumnCount() {
        	return columnNames.length;
        }

        public int getRowCount() {
        	if (data == null) return 0;
        	return data.length;
        }

        public String getColumnName(int col) {
        	return columnNames[col];
        }

        // Returns the object at the indicated position
        public Object getValueAt(int row, int col) {
        	if (data == null) return 0;
            return data[row][col];
        }

        // Turns booleans into checkboxes.  Don't ask.  I don't know how it works myself.
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        // The table cannot be edited by the user, no matter what.
        public boolean isCellEditable(int row, int col) {
           	return false;
        }

        // Edits the value at the indicated position
        public void setValueAt(Object value, int row, int col) {
        	if (data == null) return;
            data[row][col] = value;
        }
    }

    class DeparturesTableModel extends AbstractTableModel {
    	// Three different columns:  User's name, position, and online status
    	String[] columnNames = {"Plane ID", "Destination", "Runway", "Location", "Status","Ground Control Clearance","Local Control Clearance","Traffic Control Clearance"};

    	// Sample data.  Data entered is a two dimensional array of objects.
    	Object[][] data = {
    			{"GFS102","Hobby","KAP303","Runway 3","Landing",new Boolean(true),new Boolean(false),new Boolean(true)},
    			{"SGM304","JFK","SAL200","Runway 4","In Air",new Boolean(true),new Boolean(false),new Boolean(true)},
    	};

    	// Essential functions

        public int getColumnCount() {
        	return columnNames.length;
        }

        public int getRowCount() {
        	if (data == null) return 0;
        	return data.length;
        }

        public String getColumnName(int col) {
        	return columnNames[col];
        }

        // Returns the object at the indicated position
        public Object getValueAt(int row, int col) {
        	if (data == null) return null;
            return data[row][col];
        }

        // Turns booleans into checkboxes.  Don't ask.  I don't know how it works myself.
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        // The table cannot be edited by the user, no matter what.
        public boolean isCellEditable(int row, int col) {
           	return false;
        }

        // Edits the value at the indicated position
        public void setValueAt(Object value, int row, int col) {
        	if (data == null) return;
            data[row][col] = value;
        }
    }
}